package cn.lingnan.controller.admin;


import cn.lingnan.pojo.dto.PropertyDTO;
import cn.lingnan.pojo.entity.PageBean;
import cn.lingnan.pojo.vo.TransactionVO;
import cn.lingnan.pojo.vo.UserVO;
import cn.lingnan.service.AdminService;
import cn.lingnan.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;


    @GetMapping("/getHouseTransaction")
    public Result AdminGetNewHouseTransaction(Integer type){
        log.info("管理员查询订单信息");
        List<TransactionVO> transactionVO=  adminService.getHouseTransaction(type);
        return Result.success(transactionVO);
    }

    @PutMapping("/agreeTransaction")
    public Result AdminAgreeTransaction(Long transactionId){
        log.info("管理员同意订单");
        adminService.agreeTransaction(transactionId);
        return Result.success();
    }

    @PostMapping("/savePropertyInfo")
    public Result AdminSavePropertyInfo(@RequestBody PropertyDTO propertyDTO){
        log.info("管理员保存房源信息");
        adminService.savePropertyInfo(propertyDTO);
        return Result.success();

    }

    @GetMapping("/getAllUserInfo")
    public Result<PageBean> AdminGetAllUserInfo(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize){
        log.info("管理员查询所有用户信息");
//        List<UserVO> userVO=  adminService.getAllUserInfo();
        PageBean pageResult= adminService.getAllUserInfo(page,pagesize);
        return Result.success(pageResult);

    }

    @PutMapping("/toggleUserStatus")
    public Result AdminToggalUserStatus(Long userId,Integer status){
       log.info("管理员修改用户状态,userId={},status={}", userId, status);
       adminService.toggleUserStatus(userId,status);
       return Result.success();

   }

   @PostMapping("/deleteUser")
   public Result AdminDeleteUser(@RequestBody Long[] userIds){
        log.info("管理员删除用户,userIds={}", userIds.length);
         adminService.deleteUser(userIds);
        return Result.success();
   }

   @GetMapping("/getUserInfoById")
   public Result<UserVO> AdminGetUserInfoById(Long userId){
        log.info("管理员查询用户信息,userId={}", userId);
        UserVO userVO= adminService.getUserInfoById(userId);
        return Result.success(userVO);
   }

}
